package com.heer.gymnasium.module.my.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.util.StringUtils;
import com.zhihuiyun.library.commonadapter.QuickAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.pickerview.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTouch;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan on 2018/1/24.
 * 个人资料设置界面
 */

public class InfoSetActivity extends BaseActivity {


    private static final int EDITTEXT_ID = R.id.layout_infoset_et;
    private static final int TEXTVIEW_ID = R.id.layout_infoset_tv;

    private static final int RES_LAYOUT_TEXT = R.layout.item_text_with_arrow;
    private static final int RES_LAYOUT_TEXT_ID_TEXT = R.id.item_text_with_arrow_title_tv;
    private static final int RES_LAYOUT_TEXT_ID_ARROW = R.id.item_text_with_arrow_right_iv;


    private int from = 0;
    @BindView(EDITTEXT_ID)
    EditText editText;

    private InfoSetActivity $this;
    private TimePickerView pvTime;
    private PopupWindow dropDownListDialog;

    public interface IInfoConstants {
        //        1是手机号跳转，2是身高，3是体重
        int PHONE = 1;
        int HEIGHT = 2;
        int WEIGHT = 3;
        int BIRTHDAY = 4;
        int NICKNAME = 5;
        int SEX = 6;
        int TAGME = 7;

        String INTENT_SIGN = "from";
    }

    public static void startActivityForResult(Activity context, int from) {
        Intent intent = new Intent(context, InfoSetActivity.class);
        intent.putExtra(IInfoConstants.INTENT_SIGN, from);
        context.startActivityForResult(intent, from);
    }

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(R.layout.layout_infoset);

        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        //时间选择器
        pvTime = new TimePickerView.Builder($this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                /*btn_Time.setText(getTime(date));*/
                editText.setText(getTime(date));
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, false, false, false, false})
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setDate(selectedDate)
                .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
                .setDecorView(null)
                .build();
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(date);
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.startMainActivity(this);
        from = getIntent().getIntExtra(IInfoConstants.INTENT_SIGN, 0);

    }

    @OnClick(TEXTVIEW_ID)
    public void onClick(View view) {

        if (!TextUtils.isEmpty(editText.getText())) {
            setuserInfo();
        }

    }

    @OnTouch(EDITTEXT_ID)
    public boolean touchText() {
        if (from == IInfoConstants.BIRTHDAY) {
            editText.setInputType(InputType.TYPE_NULL);
            pvTime.show();
            return true;
        } else if (from == IInfoConstants.HEIGHT) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else if (from == IInfoConstants.WEIGHT) {
            editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        } else if (from == IInfoConstants.PHONE) {
            editText.setInputType(InputType.TYPE_CLASS_PHONE);
        } else if (from == IInfoConstants.SEX) {
            editText.setInputType(InputType.TYPE_NULL);
            showSexDropDownDialog();
            return true;
        }
        return false;
    }

    //设置用户信息
    private void setuserInfo() {
        String birthday = "", weight = "", height = "", nickname = "", sex = "", tag_me = "";
        if (from == IInfoConstants.WEIGHT) {
            weight = editText.getText().toString();
        } else if (from == IInfoConstants.HEIGHT) {
            height = editText.getText().toString();
        } else if (from == IInfoConstants.BIRTHDAY) {
            birthday = editText.getText().toString();
            birthday = StringUtils.to10TimeStampStrFromYYYY_MM_DD(birthday);
        } else if (from == IInfoConstants.SEX) {
            sex = editText.getText().toString();
            if (TextUtils.isEmpty(sex)) {
                ToastUtil.show($this, "请选择性别");
                return;
            } else if (TextUtils.equals("男", sex)) {
                sex = "1";
            } else {
                sex = "2";
            }
        } else if (from == IInfoConstants.NICKNAME) {
            nickname = editText.getText().toString();
        }

        mAppHelper.apiService().setUserInfo(birthday, weight, height, nickname, sex, tag_me).enqueue(new BaseCallback<ApiResponse<String>>() {

            @Override
            public void onSuccess(Call<ApiResponse<String>> call, Response<ApiResponse<String>> response) {
                Intent intent = new Intent();
                intent.putExtra(IInfoConstants.INTENT_SIGN, editText.getText().toString());
                //TODO 保存操作未完善
                setResult(from, intent);
                ToastUtil.show($this, "保存成功");
                finish();
            }
        });
    }

    private void showSexDropDownDialog() {
        if (dropDownListDialog == null) {
            ListView listView = new ListView($this);
            dropDownListDialog = new PopupWindow();
            final ArrayList<String> list = new ArrayList<>();
            list.add("男");
            list.add("女");
            listView.setAdapter(new QuickAdapter<String>($this, RES_LAYOUT_TEXT, list) {

                @Override
                public void bindData4View(ViewHolder holder, final String data, int pos) {
                    holder.setText(RES_LAYOUT_TEXT_ID_TEXT, data);
                    holder.setVisibility(RES_LAYOUT_TEXT_ID_ARROW, View.GONE);
                    holder.getParentView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            editText.setText(data);
                            dropDownListDialog.dismiss();
                        }
                    });
                }
            });

            dropDownListDialog.setContentView(listView);
            dropDownListDialog.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            dropDownListDialog.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        dropDownListDialog.showAsDropDown(editText, 0, 20);
    }
}
